package com.mercury.pattern._01_singleton.hungry.type02;

/**
 * 饿汉式2：静态块方式
 */
public class SingletonTest02 {
    //测试
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println(instance.hashCode());
        System.out.println(instance2.hashCode());
    }
}

class Singleton{
    /**
     * 1、私有构造方法
     * 2、私有的对象，在静态块中创建
     * 3、通过静态方法返回该对象
     */

    private static Singleton instance;

    static {
        instance = new Singleton();
    }

    private Singleton(){

    }

    public static Singleton getInstance(){
        return instance;
    }

}
